package com.fuqi.android.zhongcai.base.view.activity;


import android.annotation.SuppressLint;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.error.VolleyError;
import com.fuqi.android.zhongcai.R;
import com.fuqi.android.zhongcai.base.view.adapter.CityListAdapter;
import com.fuqi.android.zhongcai.base.view.adapter.SearchResultAdapter;
import com.fuqi.android.zhongcai.base.Constants;
import com.fuqi.android.zhongcai.base.request.api.GetStaffApi;
import com.fuqi.android.zhongcai.base.request.result.GetStaffResult;
import com.fuqi.android.zhongcai.sql.Worker;
import com.fuqi.android.zhongcai.sql.dao.SQLDao;
import com.fuqi.android.zhongcai.base.view.widget.SideLetterBar;
import com.fuqi.android.zhongcai.base.view.widget.TitleBar;
import com.fuqi.android.zhongcai.utils.ActivityUtils;
import com.fuqi.framework.network.code.HandleCode;
import com.fuqi.framework.network.http.RequestManager;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

public class WorkerManagerActivity extends BaseActivity implements View.OnClickListener {


    @Bind(R.id.et_search)
    EditText etSearch;
    @Bind(R.id.iv_search_clear)
    ImageView ivSearchClear;
    @Bind(R.id.listview_all_city)
    ListView listviewAllCity;
    @Bind(R.id.tv_letter_overlay)
    TextView tvLetterOverlay;
    @Bind(R.id.side_letter_bar)
    SideLetterBar sideLetterBar;
    @Bind(R.id.listview_search_result)
    ListView listviewSearchResult;
    @Bind(R.id.empty_view)
    LinearLayout emptyView;
    private CityListAdapter mWorkersAdapter;
    private SearchResultAdapter mSearchResultAdapter;


    @Override
    public void initView() {

        titleBar.setTitle("工人管理");

        sideLetterBar.setOverlay(tvLetterOverlay);
        sideLetterBar.setOnLetterChangedListener(new SideLetterBar.OnLetterChangedListener() {
            @Override
            public void onLetterChanged(String letter) {
                int position = mWorkersAdapter.getLetterPosition(letter);
                listviewAllCity.setSelection(position);
            }
        });

        etSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                String keyword = s.toString();
                if (TextUtils.isEmpty(keyword)) {
                    ivSearchClear.setVisibility(View.GONE);
                    emptyView.setVisibility(View.GONE);
                    listviewSearchResult.setVisibility(View.GONE);
                } else {
                    ivSearchClear.setVisibility(View.VISIBLE);
                    listviewSearchResult.setVisibility(View.VISIBLE);
                    List<Worker> result = SQLDao.searchWorkerByKeyword(keyword);
                    if (result == null || result.size() == 0) {
                        emptyView.setVisibility(View.VISIBLE);
                    } else {
                        emptyView.setVisibility(View.GONE);
                        mSearchResultAdapter.changeData(result);
                    }
                }
            }
        });

        listviewSearchResult.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ActivityUtils.toast(mSearchResultAdapter.getItem(position).getName());
            }
        });

        ivSearchClear.setOnClickListener(this);
    }


    @SuppressLint("RtlHardcoded")
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_search_clear:
                etSearch.setText("");
                ivSearchClear.setVisibility(View.GONE);
                emptyView.setVisibility(View.GONE);
                listviewSearchResult.setVisibility(View.GONE);
                break;
            default:
                break;
        }
    }



    @Override
    public void initListener() {

        titleBar.addAction(new TitleBar.ImageAction(R.mipmap.btn_add) {
            @Override
            public void performAction(View view) {

                ActivityUtils.startActivity(WorkerManagerActivity.this, AddWorkerActivity.class);
            }
        });

    }

    @Override
    public void initData() {

        SQLDao.getInstance(WorkerManagerActivity.this).clearWorker();
        List<Worker> mAllWorkers = new ArrayList<>();
//        Worker worker0 = new Worker("碧玉", "biyu");
//        mAllWorkers.add(worker0);
//        SQLDao.getInstance(WorkerManagerActivity.this).insertWorker(worker0);
//        Worker worker1 = new Worker("白玉", "baiyu");
//        mAllWorkers.add(worker1);
//        SQLDao.getInstance(WorkerManagerActivity.this).insertWorker(worker1);
//        Worker worker2 = new Worker("白花", "baihua1");
//        mAllWorkers.add(worker2);
//        SQLDao.getInstance(WorkerManagerActivity.this).insertWorker(worker2);
//        Worker worker3 = new Worker("陈雪", "chenxue");
//        mAllWorkers.add(worker3);
//        SQLDao.getInstance(WorkerManagerActivity.this).insertWorker(worker3);
//        Worker worker4 = new Worker("陈彤", "chntong");
//        mAllWorkers.add(worker4);
//        SQLDao.getInstance(WorkerManagerActivity.this).insertWorker(worker4);
//        Worker worker5 = new Worker("周伯通", "zhoubotong");
//        mAllWorkers.add(worker5);
//        SQLDao.getInstance(WorkerManagerActivity.this).insertWorker(worker5);
        mWorkersAdapter = new CityListAdapter(WorkerManagerActivity.this, mAllWorkers);
        mWorkersAdapter.setOnCityClickListener(new CityListAdapter.OnCityClickListener() {
            @Override
            public void onItemClick(String name) {

                ActivityUtils.toast(name);

            }

        });

        mSearchResultAdapter = new SearchResultAdapter(WorkerManagerActivity.this, null);
        listviewAllCity.setAdapter(mWorkersAdapter);
        listviewSearchResult.setAdapter(mSearchResultAdapter);
        RequestManager.getInstance().call(new GetStaffApi(new GetStaffApi.GetStaffParams(Constants.ROLE_SGGR),
                respWorkersListener, errorListener));
    }



    Response.Listener<GetStaffResult[]> respWorkersListener = new Response.Listener<GetStaffResult[]>() {

        @Override
        public void onResponse(GetStaffResult[] response) {

            if (HandleCode.requestSuccess()) {

                if (response != null && response.length > 0){

                    mWorkersAdapter.loadMoreData(response);
                }


                Log.i("juner", "juner respListener response : ");
            } else {



            }
        }
    };


    Response.ErrorListener errorListener = new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {

            Log.i("juner", "juner  error.getMessage(): " + error.getMessage());

        }
    };



    @Override
    public boolean hasTitleBar() {
        return true;
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_worker_manager;
    }


}
